﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

using Liquid;

using SBPweb.Communication.Shared.Library.Conversations;

using SBPweb.Communication.Client.Library.Xmpp.Conversations;

namespace SBPweb.Communication.Client.Controls.Xmpp.Conversations
{

    public partial class ConversationView : UserControl
    {

        #region Private fields

        private ConversationBase conversation;

        #endregion

        #region Constructor logic

        public ConversationView()
        {
            InitializeComponent();
        }

        public ConversationView(ConversationBase conversation)
            : this()
        {
            Conversation = conversation;
        }

        #endregion

        #region Property accessors

        public ConversationBase Conversation
        {
            get
            {
                return conversation;
            }
            set
            {
                if (conversation != null)
                {
                    conversation.MessageReceived -= new EventHandler<MessageBaseEventArgs>(conversation_MessageReceived);
                }
                conversation = value;
                conversation.MessageReceived += new EventHandler<MessageBaseEventArgs>(conversation_MessageReceived);
                Tag = conversation.Title;
            }
        }

        #endregion

        #region Methods

        private void SendOutgoingMessage()
        {
            if (conversation == null)
            {
                return;
            }
            conversation.SendMessage(tbOutgoing.Text);
            tbOutgoing.Text = string.Empty;
        }

        #region Event handlers

        private void conversation_MessageReceived(object sender, MessageBaseEventArgs e)
        {
            this.Dispatcher.BeginInvoke(delegate
            {
                if (e.Message.IsMyOwn)
                {
                    rtbIncoming.ApplyFormatting(Formatting.Foreground, Color.FromArgb(255, 0, 0, 255));
                }
                else
                {
                    rtbIncoming.ApplyFormatting(Formatting.Foreground, Color.FromArgb(255, 0, 255, 0));
                }
                rtbIncoming.ReturnFocus();
                rtbIncoming.HTML += string.Format("<b>[{0:T}]</b>&nbsp;<i>{1}</i>&nbsp;says:&nbsp;{2}", e.Message.TimeStamp, e.Message.From.DisplayName, e.Message.Body);
                //rtbIncoming.ApplyFormatting(Formatting.Bold, null);
                //rtbIncoming.ReturnFocus();
                //rtbIncoming.Text += string.Format("\n[{0:T}]", e.Message.TimeStamp);
                //rtbIncoming.ApplyFormatting(Formatting.RemoveBold | Formatting.Italic, null);
                //rtbIncoming.ReturnFocus();
                //rtbIncoming.Text += string.Format(" {0} says: ", e.Message.From.DisplayName);
                //rtbIncoming.ApplyFormatting(Formatting.RemoveItalic, null);
                //rtbIncoming.ReturnFocus();
                //rtbIncoming.Text += e.Message.Body;

                //spIncoming.Children.Add(new MessageView(e.Message));
            });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            SendOutgoingMessage();
        }

        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                SendOutgoingMessage();
            }
        }

        #endregion

        #endregion
        
    }

}